<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body, html {
            height: 100%;
            font-family: 'Microsoft YaHei', sans-serif;
            background: url('img/paperwall_2.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-container {
            background: rgba(255, 255, 255, 0.8);
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
            width: 100%;
            max-width: 400px;
            text-align: center;
        }
        .login-container h2 {
            margin-bottom: 30px;
            color: #333;
            font-size: 26px;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 600;
        }
        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 14px;
        }
        .btn {
            width: 100%;
            padding: 12px;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
        }
        .btn-primary {
            background-color: #4D89FF;
            color: #fff;
        }
        .btn-primary:hover {
            background-color: #2C5B8D;
        }
        .btn-secondary {
            background-color: #aaa;
            color: #fff;
        }
        .error-message, .success-message {
            font-size: 14px;
            margin-top: 8px;
            color: red;
            display: none;
        }
        .success-message {
            color: green;
        }
        .forgot-password {
            display: block;
            margin-top: 15px;
            color: #4D89FF;
            text-decoration: none;
            font-size: 14px;
        }
        .forgot-password:hover {
            text-decoration: underline;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0; left: 0;
            width: 100%; height: 100%;
            background: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
        }
        .modal-content {
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            width: 90%;
            max-width: 400px;
        }
        .modal-header h3 {
            margin-bottom: 20px;
        }
        .modal-footer {
            display: flex;
            gap: 10px;
            margin-top: 20px;
        }
        .modal-footer .btn {
            flex: 1;
        }
        .error-message {
            color: red;
            font-size: 14px;
            display: none;
        }

        .success-message {
            margin-top: 10px;
            font-size: 14px;
        }

        .forgot-password,
        .register-link {
            display: block;
            margin-top: 10px;
            text-align: right;
            font-size: 14px;
            color: #007bff;
            cursor: pointer;
        }

        /* 简易模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 999;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.4);
        }

        .modal-content {
            background-color: #fefefe;
            margin: 10% auto;
            padding: 20px;
            width: 90%;
            max-width: 400px;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.2);
        }

        .close {
            float: right;
            font-size: 20px;
            cursor: pointer;
        }

        .form-group {
            margin-bottom: 10px;
        }

        .form-control {
            width: 100%;
            padding: 8px;
            margin-top: 4px;
        }

        .btn {
            padding: 8px 16px;
            width: 100%;
        }
    </style>
</head>
<body>

<div class="login-container">
    <h2>用户登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="userName">用户名</label>
            <input type="text" id="userName" class="form-control" required>
            <div id="userNameError" class="error-message"></div>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" class="form-control" required>
            <div id="passwordError" class="error-message"></div>
        </div>
        <button type="submit" class="btn btn-primary">登录</button>
        <div id="successMessage" class="success-message"></div>
        <a class="forgot-password" id="forgotPasswordLink">忘记密码？</a>
        <a class="register-link" href="register.html">注册账号？</a>
    </form>
</div>

<!-- 忘记密码模态框 -->
<div class="modal" id="resetPasswordModal">
    <div class="modal-content">
        <span class="close" id="closeModalBtn">&times;</span>
        <h3>重置密码</h3>
        <div class="form-group">
            <label for="resetUserName">用户名</label>
            <input type="text" id="resetUserName" class="form-control">
        </div>
        <div class="form-group">
            <label for="resetpid">身份证号</label>
            <input type="text" id="resetpid" class="form-control">
        </div>
        <div class="form-group">
            <label for="resetEmail">邮箱</label>
            <input type="email" id="resetEmail" class="form-control">
        </div>
        <div class="form-group">
            <label for="resetNewPassword">新密码</label>
            <input type="password" id="resetNewPassword" class="form-control">
        </div>
        <button id="resetPasswordBtn" class="btn btn-primary">提交重置</button>
        <div id="resetMessage" class="success-message"></div>
    </div>
</div>

<script src="Js/jquery.min.3.6.0.js"></script>
<script>
    $(document).ready(function () {
        $('#loginForm').submit(function (e) {
            e.preventDefault();
            $('.error-message').hide();

            const userName = $('#userName').val().trim();
            const password = $('#password').val().trim();

            if (!userName) {
                $('#userNameError').text('请输入用户名').show();
                return;
            }
            if (!password) {
                $('#passwordError').text('请输入密码').show();
                return;
            }

            $('button[type="submit"]').text('登录中...').prop('disabled', true);

            $.ajax({
                url: '/sysUser/login',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ userName, password }),
                success: function (response) {
                    if (response.success) {
                        localStorage.setItem('roleId', response.roleId);
                        localStorage.setItem('userName', response.userName);
                        localStorage.setItem('userId', response.userId);
                        $('#successMessage').text('登录成功！').css('color', 'green').show();
                        setTimeout(() => window.location.href = 'home.html', 1000);
                    } else {
                        $('#successMessage').text(response.message).css('color', 'red').show();
                    }
                },
                error: function () {
                    $('#successMessage').text('登录失败，请重试').css('color', 'red').show();
                },
                complete: function () {
                    $('button[type="submit"]').text('登录').prop('disabled', false);
                }
            });
        });

        // 打开模态框
        $('#forgotPasswordLink').click(function () {
            $('#resetPasswordModal').fadeIn();
        });

        // 关闭模态框
        $('#closeModalBtn').click(function () {
            $('#resetPasswordModal').fadeOut();
            $('#resetMessage').text('');
        });

        // 提交重置密码请求
        $('#resetPasswordBtn').click(function () {
            const userName = $('#resetUserName').val().trim();
            const pid = $('#resetpid').val().trim();
            const email = $('#resetEmail').val().trim();
            const newPassword = $('#resetNewPassword').val().trim();

            if (!userName || !pid || !email || !newPassword) {
                $('#resetMessage').text('请填写所有字段').css('color', 'red');
                return;
            }

            $.ajax({
                url: '/sysUser/resetPassword',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ userName, pid, email, newPassword }),
                success: function (response) {
                    if (response.success) {
                        $('#resetMessage').text('密码重置成功，请重新登录').css('color', 'green');
                        setTimeout(() => $('#resetPasswordModal').fadeOut(), 1500);
                    } else {
                        $('#resetMessage').text(response.message).css('color', 'red');
                    }
                },
                error: function () {
                    $('#resetMessage').text('请求失败，请稍后再试').css('color', 'red');
                }
            });
        });
    });
</script>
</body>
</html>


